package com.itboye.hutouben.activity.mysetting;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.itboye.hutouben.R;
import com.itboye.hutouben.adapter.WeiXiuRecordAdapter;
import com.itboye.hutouben.base.BaseActivity;
import com.itboye.hutouben.base.ptr.BasePtr;
import com.itboye.hutouben.bean.WeiXiuiJiLuBean;
import com.itboye.hutouben.presenter.SystemMessgeAPresenter;
import com.itboye.hutouben.util.IsUtilUid;
import com.itboye.hutouben.volley.ResultEntity;

import java.util.ArrayList;
import java.util.List;
import java.util.Observable;
import java.util.Observer;

import in.srain.cube.views.ptr.PtrDefaultHandler2;
import in.srain.cube.views.ptr.PtrFrameLayout;

/**
 * 维修记录
 */
public class WeiXiuRecord extends BaseActivity implements Observer  {
    ListView my_list;
    TextView add_shap_title_tv;
    WeiXiuRecordAdapter adapter;
    ImageView close_icon;
    SystemMessgeAPresenter presenter;
    WeiXiuiJiLuBean bean;
    PtrFrameLayout commend_anchor_ptr;//设置刷新
    LinearLayout lineweixiu;
    int pageNum=1;
    int pageSize=10;
    List<WeiXiuiJiLuBean.ListInfo> beanList=new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_wei_xiu_record);
        my_list = (ListView) findViewById(R.id.my_list);
        presenter = new SystemMessgeAPresenter(this);
        bean = new WeiXiuiJiLuBean();
        showProgressDialog("数据加载中...",true);
        presenter.onWeiXiuJiLu(IsUtilUid.isUid(), IsUtilUid.isGoup(),"8",pageNum+"", pageSize+"");
        beanList=new ArrayList<WeiXiuiJiLuBean.ListInfo>();
        adapter = new WeiXiuRecordAdapter(WeiXiuRecord.this, beanList);
        my_list.setAdapter(adapter);
        BasePtr.setPagedPtrStyle(commend_anchor_ptr);//设置上拉下拉刷新
        commend_anchor_ptr.setPtrHandler(new PtrDefaultHandler2() {//刷新的监听事件
            @Override
            public void onLoadMoreBegin(PtrFrameLayout frame) {
                pageNum++;
                presenter.onWeiXiuJiLu(IsUtilUid.isUid(), IsUtilUid.isGoup(),"8",pageNum+"", pageSize+"");
            }

            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                presenter.onWeiXiuJiLu(IsUtilUid.isUid(), IsUtilUid.isGoup(),"8",pageNum+"", pageSize+"");
            }
        });
        commend_anchor_ptr.autoRefresh();

        add_shap_title_tv.setText("维修记录");
    }

    @Override
    protected void onInit() {

    }

    @Override
    protected void onReadIntent(Bundle bundle) {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.close_icon:
                finish();
                break;
        }
    }

    @Override
    public void update(Observable observable, Object data) {

        ResultEntity entity = handlerError(data);
        if (entity != null) {
            IsUtilUid.tianjia(entity);
            if (entity.getEventType() == presenter.WeiXiuJilu_success) {
                if(pageNum==1){
                    beanList.clear();
                }
                commend_anchor_ptr.refreshComplete();//设置刷新完成
                WeiXiuiJiLuBean   bean = (WeiXiuiJiLuBean) entity.getData();
                beanList.addAll(bean.getList());

                if(beanList.size()<=0){
                    lineweixiu.setVisibility(View.VISIBLE);
                    my_list.setVisibility(View.GONE);
                    commend_anchor_ptr.setVisibility(View.GONE);
                }else{
                    adapter.notifyDataSetChanged();
                    lineweixiu.setVisibility(View.GONE);
                    commend_anchor_ptr.setVisibility(View.VISIBLE);
                    my_list.setVisibility(View.VISIBLE);
                }
            }
        }
        try {
            closeProgressDialog();
        }catch (Exception ex){

        }
    }

}
